<template>
<!-- 登录页面 -->
<div class="login-container">
  <div class="login-box">
    <el-form :model="loginForm" class="login-form">
      <el-form-item>
        <el-input 
          v-model="loginForm.username"
          placeholder="请输入用户名"
          prefix-icon="User"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="loginForm.password" 
          type="password"
          placeholder="请输入密码"
          prefix-icon="Lock"
          show-password
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="login-btn" @click="handleLogin">
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>
<script setup >
import {ref,reactive} from "vue"
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const loginFormRef = ref(null)
const router = useRouter()

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    
    // TODO: 调用登录接口
    const res = await login({
      username: loginForm.username,
      password: loginForm.password 
    })
    
    if(res?.code === 0) {
      // 登录成功
      ElMessage.success('登录成功')
      // 存储token
      localStorage.setItem('token', res.data?.token)
      // 存储token到pinia
      useUserStore().setToken(res.data?.token)
      // 跳转到首页
      router.push('/base/home')
    } else {
      ElMessage.error(res?.message || '登录失败')
    }
    
  } catch(error) {
    console.error('登录失败:', error)
    ElMessage.error('表单校验失败')
  }
}


</script>
<style lang="scss" scoped>

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.login-box {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 10px;
}
h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #409EFF;
  font-size: 28px;
}

.login-form {
  .el-form-item {
    margin-bottom: 25px;
  }

  .el-input {
    .el-input__wrapper {
      padding: 1px 15px;
      height: 42px;
      background: #f5f7fa;
      border-radius: 4px;
      border: 1px solid #dcdfe6;

      &:hover {
        border-color: #c0c4cc;
      }

      &.is-focus {
        border-color: #409EFF;
        background: #fff;
      }
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    font-size: 16px;
    letter-spacing: 2px;
    background: #409EFF;
    border: none;
    
    &:hover {
      background: #66b1ff;
    }
    
    &:active {
      background: #3a8ee6;
    }
  }
}

.login-box {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
  
  &:hover {
    box-shadow: 0 4px 16px 0 rgba(0, 0, 0, 0.2);
  }
}

.login-container {
  background: linear-gradient(135deg, #7367F0, #CE9FFC);
}


</style>
